<?php

namespace app\admin\controller;

use app\BaseController;
use app\admin\model\Type as TypeModel;

class Type extends BaseController
{
    public function list()
    {
        if (!$this->request->auth) return show(config('status.auth'), '认证失败');
        $names = TypeModel::where('pid', 0)->column('name');
        $list = TypeModel::whereIn('pid', [1, 2, 3, 4, 5, 6])->order('sort asc')->select()->toArray();
        $list1 = $list2 = $list3 = $list4 = $list5 = $list6 = [];
        foreach ($list as $v) {
            switch ($v['pid']) {
                case 1:
                    $list1[] = $v;
                    break;

                case 2:
                    $list2[] = $v;
                    break;

                case 3:
                    $list3[] = $v;
                    break;

                case 4:
                    $list4[] = $v;
                    break;

                case 5:
                    $list5[] = $v;
                    break;

                case 6:
                    $list6[] = $v;
                    break;
            }
        }

        $data = [$names, $list1, $list2, $list3, $list4, $list5, $list6];
        return show(200, '查询成功', $data);
    }

    public function add()
    {
        if (!$this->request->auth) return show(config('status.auth'), '认证失败');
        $radio1 = input('radio1', '');
        $radio2 = input('radio2', '');

        if ($radio2 == '欣赏') {
            if ($radio1 == '视频') {
                $pid = 1;
            } else if ($radio1 == '音频') {
                $pid = 3;
            } else if ($radio1 == '文章') {
                $pid = 5;
            }
        } else if ($radio2 == '展示') {
            if ($radio1 == '视频') {
                $pid = 2;
            } else if ($radio1 == '音频') {
                $pid = 4;
            } else if ($radio1 == '文章') {
                $pid = 6;
            }
        }

        $name = input('name', '');
        $sort = input('sort', 0, 'intval');
        $result = TypeModel::create([
            'pid'  =>  $pid,
            'name' =>  $name,
            'sort' =>  $sort
        ]);
        if ($result) {
            return show(200, '添加成功');
        } else {
            return show(config('status.error_add'), '添加失败');
        }
    }

    public function edit()
    {
        if (!$this->request->auth) return show(config('status.auth'), '认证失败');
        $id = input('id', 0, 'intval');
        $data = TypeModel::where('id', $id)->find()->toArray();
        return show(200, '查询成功', $data);
    }

    public function modify()
    {
        if (!$this->request->auth) return show(config('status.auth'), '认证失败');
        $id = input('id', 0, 'intval');
        $radio1 = input('radio1', '');
        $radio2 = input('radio2', '');

        if ($radio2 == '欣赏') {
            if ($radio1 == '视频') {
                $pid = 1;
            } else if ($radio1 == '音频') {
                $pid = 3;
            } else if ($radio1 == '文章') {
                $pid = 5;
            }
        } else if ($radio2 == '展示') {
            if ($radio1 == '视频') {
                $pid = 2;
            } else if ($radio1 == '音频') {
                $pid = 4;
            } else if ($radio1 == '文章') {
                $pid = 6;
            }
        }

        $name = input('name', '');
        $sort = input('sort', 0, 'intval');
        $result = TypeModel::update([
            'id' => $id,
            'pid' => $pid,
            'name' => $name,
            'sort' => $sort
        ]);
        if ($result) {
            return show(200, '修改成功');
        } else {
            return show(config('status.error_update'), '修改失败');
        }
    }

    public function modifyName()
    {
        if (!$this->request->auth) return show(config('status.auth'), '认证失败');
        $id = input('id', 0, 'intval');
        $name = input('name', '');
        if ($id == 1 || $id == 2) {
            $name = '视频' . $name;
        }

        if ($id == 3 || $id == 4) {
            $name = '音频' . $name;
        }

        if ($id == 5 || $id == 6) {
            $name = '文章' . $name;
        }

        $result = TypeModel::update([
            'id' => $id,
            'name' => $name
        ]);
        if ($result) {
            return show(200, '修改成功');
        } else {
            return show(config('status.error_update'), '修改失败');
        }
    }

    public function delete()
    {
        if (!$this->request->auth) return show(config('status.auth'), '认证失败');
        $id = input('id', 0, 'intval');
        $result = TypeModel::destroy($id);
        if ($result) {
            return show(200, '删除成功');
        } else {
            return show(config('status.error_delete'), '删除失败');
        }
    }

    public function getOne()
    {
        if (!$this->request->auth) return show(config('status.auth'), '认证失败');
        $id = input('id', 0, 'intval');
        $data = TypeModel::where('pid', $id)->order('sort asc')->select()->toArray();
        return show(200, '分类列表', $data);
    }
}
